package com.base.syslib.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.SparseIntArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

/**
 * Created by fangzhu
 * 处理嵌套gradivew 不显示全的问题
 * 处理嵌套gradivew 出现点击冲突的解决办法
 * 在最外层的item布局中加上
 * android:descendantFocusability="blocksDescendants"
 * 不影响各自的onItemClick点击事件
 */
public class MyGridView extends GridView {
    /*isMeasure in scrollView to get height*/
    boolean isMeasure = true;
    /*touchEvent*/
    boolean isTouchEvent = false;

    public MyGridView(Context context) {
        super(context);
    }

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /*get data size to measureSpec of listview height*/
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (isMeasure) {
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public boolean onInterceptTouchEvent(MotionEvent ev) {

        return isTouchEvent;
    }

    public boolean isTouchEvent() {
        return isTouchEvent;
    }

    public void setTouchEvent(boolean touchEvent) {
        isTouchEvent = touchEvent;
    }

    public boolean isMeasure() {
        return isMeasure;
    }

    public void setIsMeasure(boolean isMeasure) {
        this.isMeasure = isMeasure;
    }


    /**
     * 存储宽度
     */
    static SparseIntArray mGvWidth = new SparseIntArray();

    /**
     * 计算GridView的高度
     *
     * @param gridView 要计算的GridView
     */
    public static void updateGridViewLayoutParams(MyGridView gridView, int maxColumn) {
        int childs = gridView.getAdapter().getCount();
        if (childs > 0) {
            int columns = childs < maxColumn ? childs % maxColumn : maxColumn;
            gridView.setNumColumns(columns);
            int width = 0;
            int cacheWidth = mGvWidth.get(columns);
            if (cacheWidth != 0) {
                width = cacheWidth;
            } else { // 计算gridview每行的宽度, 如果item小于3则计算所有item的宽度;
                // 否则只计算3个child宽度，因此一行最多3个child。 (这里我们以3为例)
                int rowCounts = childs < maxColumn ? childs : maxColumn;
                for (int i = 0; i < rowCounts; i++) {
                    View childView = gridView.getAdapter().getView(i, null, gridView);
                    childView.measure(0, 0);
                    width += childView.getMeasuredWidth();
                }
            }
            ViewGroup.LayoutParams params = gridView.getLayoutParams();
            params.width = width;
            gridView.setLayoutParams(params);
            if (mGvWidth.get(columns) == 0) {
                mGvWidth.append(columns, width);
            }
        }
    }


}
